import 'package:flutter/material.dart';
import 'package:flutter_house_manager/pages/House/house_detail.dart';
import 'package:flutter_house_manager/pages/House/house_list.dart';
import 'package:flutter_house_manager/pages/Location/house_form.dart';
import 'package:flutter_house_manager/pages/Location/build_list.dart';
import 'package:flutter_house_manager/pages/Location/location_list.dart';
import 'package:flutter_house_manager/pages/Location/room_list.dart';
import 'package:flutter_house_manager/pages/Login/index.dart';
import 'package:flutter_house_manager/pages/NotFound/not_found.dart';
import 'package:flutter_house_manager/pages/Profile/index.dart';
import 'package:flutter_house_manager/pages/TabsPage/index.dart';
import 'package:flutter_house_manager/pages/NoticeDetail/index.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    title: '房屋管理系统',
    initialRoute: '/',
    routes: {
      '/': (context) => const TabsPage(),
      '/noticeDetail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
      // '/profile': (context) => const ProfilePage(),
    },
    // onGenerateRoute 不能有异步，只能是同步
    onGenerateRoute: (settings) {
      // 1.获取token
      String? token = tokenManager.getToken();
      // 2.没有token 跳转登录页面
      if (token.isEmpty) {
        return MaterialPageRoute(
            builder: (context) => LoginPage(toName: settings.name));
      }
      // 3.有token 跳转对应页面
      if (settings.name == '/profile') {
        return MaterialPageRoute(builder: (context) => const ProfilePage());
      }
      // 4.有token 跳转房屋列表页面
      if (settings.name == '/houselist') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }
      // 5.跳转周边小区页面
      if (settings.name == '/locationlist') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }
      // 6.跳转楼栋列表页面
      if (settings.name == '/buildinglist') {
        return MaterialPageRoute(builder: (context) => const BuildingList());
      }
      // 7.跳转房间列表页面
      if (settings.name == '/roomlist') {
        return MaterialPageRoute(builder: (context) => const RoomList());
      }
      // 8.跳转添加房屋页面
      if (settings.name == "/houseform") {
        String id = "";
        if (settings.arguments != null) {
          id = (settings.arguments as Map)["id"];
        }
        return MaterialPageRoute(
          // settings: settings,
          builder: (context) => HouseForm(id: id),
        );
      }
      // 9.跳转房屋详情页面
      if (settings.name == "/housedetail") {
        return MaterialPageRoute(
          // 路由对象
          settings: settings,
          builder: (context) => const HouseDetail(),
        );
      }

      // 兜底返回 当什么都匹配不了的时候 匹配404页面
      return MaterialPageRoute(
        builder: (context) => const NotFound(),
      );
    },
  ));
}
